<?php
require_once ZEAI.'sub/conn.php';
require_once ZEAI.'sub/zeai_up_func.php';
require_once ZEAI.'cache/config_reg.php';
//function tg_insert_init() {
//	global $_ZEAI,$_REG,$cook_uid,$db,$cook_openid;
	$TG_set = json_decode(stripslashes($_REG['TG_set']),true);
	if(ifint($cook_uid)){
		$ifed=true;
		if(!empty($cook_openid)){
			$rowtg = $db->ROW(__TBL_TG_USER__,"id,uname,nickname,mob,pwd,uid","openid='$cook_openid'","name");
			if ($rowtg){
				$cook_tg_uid   = $rowtg['id'];
				$uidd           = intval($rowtg['uid']);
				$cook_tg_uname = $rowtg['uname'];
				$cook_tg_nickname = $rowtg['nickname'];
				$cook_tg_mob   = $rowtg['mob'];
				$cook_tg_pwd   = $rowtg['pwd'];
				if(!ifint($uidd))$db->query("UPDATE ".__TBL_TG_USER__." SET uid=".$cook_uid." WHERE id=".$cook_tg_uid);
				setcookie("cook_tg_uid",$cook_tg_uid,time()+31536000,"/",$_ZEAI['CookDomain']);
				setcookie("cook_tg_uname",$cook_tg_uname,time()+31536000,"/",$_ZEAI['CookDomain']);
				setcookie("cook_tg_mob",$cook_tg_mob,time()+31536000,"/",$_ZEAI['CookDomain']);
				setcookie("cook_tg_pwd",$cook_tg_pwd,time()+31536000,"/",$_ZEAI['CookDomain']);
				setcookie("cook_tg_openid",$cook_openid,time()+315360000,"/",$_ZEAI['CookDomain']);
			}else{
				$ifed=false;
			}
		}
		if(empty($cook_openid) || !$ifed){
			$rowtg = $db->ROW(__TBL_TG_USER__,"id,uname,mob,pwd,nickname","uid=".$cook_uid,"name");
			if (!$rowtg){
				$ifadd=false;
				$rowU = $db->ROW(__TBL_USER__,"uname,pwd,mob,RZ,openid,subscribe,weixin,qq,aboutus,areaid,areatitle,nickname,photo_s,tguid","id=".$cook_uid,"name");
				if ($rowU){
					$mob= $rowU['mob'];
					$uname=$rowU['uname'];
					$pwd  = $rowU['pwd'];
					$openid    =$rowU['openid'];
					$subscribe =$rowU['subscribe'];
					$RZ      = $rowU['RZ'];$RZarr = explode(',',$RZ);
					$weixin  = $rowU['weixin'];
					$qq      = $rowU['qq'];
					$aboutus = $rowU['aboutus'];
					$areaid  = $rowU['areaid'];
					$areatitle = $rowU['areatitle'];
					$nickname  = $rowU['nickname'];
					$photo_s   = $rowU['photo_s'];
					$U_tguid   = intval($rowU['tguid']);
					if(ifmob($mob) && in_array('mob',$RZarr)){
						$rowtg2 = $db->ROW(__TBL_TG_USER__,"id,uname,mob,pwd","mob='$mob' AND FIND_IN_SET('mob',RZ)","name");
						if($rowtg2){
							$cook_tg_uid   = $rowtg2['id'];
							$cook_tg_uname = $rowtg2['uname'];
							$cook_tg_mob   = $rowtg2['mob'];
							$cook_tg_pwd   = $rowtg2['pwd'];
							setcookie("cook_tg_uid",$cook_tg_uid,time()+31536000,"/",$_ZEAI['CookDomain']);
							setcookie("cook_tg_uname",$cook_tg_uname,time()+31536000,"/",$_ZEAI['CookDomain']);
							setcookie("cook_tg_mob",$cook_tg_mob,time()+31536000,"/",$_ZEAI['CookDomain']);
							setcookie("cook_tg_pwd",$cook_tg_pwd,time()+31536000,"/",$_ZEAI['CookDomain']);
						}else{
							$ifadd=true;
						}
					}else{
						$ifadd=true;
					}
					//
					if($ifadd){
						$flag   = ($TG_set['regflag'] == 1)?0:1;
						$row2   = $db->ROW(__TBL_TG_ROLE__,"grade,title","shopgrade=0 AND ifdefault=1","num");
						$grade  = $row2[0];
						$gradetitle = $row2[1];
						$sjtime = ADDTIME;
						$ip     =getip();
						$kind   = 1;
						if($TG_set['active_price']>0)$flag=2;
						//
						$row2 = $db->ROW(__TBL_TG_USER__,"id","uname='$uname'");
						if($row2)$uname=$cook_uid;
						//
						if(!empty($photo_s)){
							$new_photo_s=str_replace("/m/","/tg/",$photo_s);
							UP_modFn('u','copy',$photo_s,$new_photo_s);
							UP_modFn('u','copy',smb($photo_s,'b'),str_replace("/m/","/tg/",smb($photo_s,'b')));
							UP_modFn('u','copy',smb($photo_s,'m'),str_replace("/m/","/tg/",smb($photo_s,'m')));
						}
						//
						$db->query("INSERT INTO ".__TBL_TG_USER__." (tguid,uid,uname,nickname,flag,pwd,grade,gradetitle,regtime,endtime,regip,endip,kind,openid,subscribe,qq,weixin,content,areaid,areatitle,photo_s) VALUES ($U_tguid,$cook_uid,'$uname','$nickname',$flag,'".$pwd."',$grade,'$gradetitle',".ADDTIME.",".ADDTIME.",'$ip','$ip',$kind,'$openid','$subscribe','$qq','$weixin','$aboutus','$areaid','$areatitle','$new_photo_s')");
						$tg_uid = intval($db->insert_id());
						if(ifmob($mob) && in_array('mob',$RZarr)){
							$db->query("UPDATE ".__TBL_TG_USER__." SET mob='$mob',RZ='mob' WHERE id=".$tg_uid);
						}
						$cook_tg_uid = $tg_uid;
						$cook_tg_pwd = $pwd;
						$cook_tg_openid= $openid;
						setcookie("cook_tg_uid",$cook_tg_uid,time()+31536000,"/",$_ZEAI['CookDomain']);
						setcookie("cook_tg_pwd",$cook_tg_pwd,time()+31536000,"/",$_ZEAI['CookDomain']);
						setcookie("cook_tg_openid",$cook_tg_openid,time()+315360000,"/",$_ZEAI['CookDomain']);
					}
					//
				}
			}else{
				$cook_tg_uid = $rowtg['id'];
				$cook_tg_uname = $rowtg['uname'];
				$cook_tg_nickname = $rowtg['nickname'];
				$cook_tg_mob = $rowtg['mob'];
				$cook_tg_pwd = $rowtg['pwd'];
				setcookie("cook_tg_uid",$cook_tg_uid,time()+31536000,"/",$_ZEAI['CookDomain']);
				setcookie("cook_tg_uname",$cook_tg_uname,time()+31536000,"/",$_ZEAI['CookDomain']);
				setcookie("cook_tg_mob",$cook_tg_mob,time()+31536000,"/",$_ZEAI['CookDomain']);
				setcookie("cook_tg_pwd",$cook_tg_pwd,time()+31536000,"/",$_ZEAI['CookDomain']);
			}
		}
	}
//}
//tg_insert_init();
?>